#ifndef MESSAGEEXHIBITION_H
#define MESSAGEEXHIBITION_H

#include <QLabel>
#include <QScrollArea>
#include <QPushButton>
#include <QWidget>
#include "Data.h"

using namespace model;


// ------------------------------------------------------------ //
// --------------------- 消息展示区 ------------------- //
// ------------------------------------------------------------ //
class MessageExhibition : public QScrollArea
{
    Q_OBJECT
public:
    MessageExhibition();

    // 插入消息
    void addMessage(bool isLeft, const Message* message); // 尾插
    void addFrontMessage(bool isLeft, const Message* message); // 头插
    void clear(); // 清空消息区
    void scrollToBottom(); // 滚动到末尾

    void addMessage(bool isLeft, const Message &message);
private:
    QWidget* container;
};

// ------------------------------------------------------------ //
// --------------------- 单个消息元素 ------------------- //
// --------------支持 文本、图片、视频、语音------------------- //
// ------------------------------------------------------------ //
class MessageItem : public QWidget
{
    Q_OBJECT
public:
    MessageItem(bool isLeft);

    // 用于创建消息
    static MessageItem* makeMessageItem(bool isLeft, const Message& message);
    // 不同消息的创建函数
    static QWidget* makeTextMessageItem(bool isLeft, const QString& text);
    static QWidget* makeImageMessage(bool isLeft, const QString& fileId, const QByteArray& content);
    static QWidget* makeFileMessageItem(bool isLeft, const Message& message);
    static QWidget* makeSpeechMessageItem(bool isLeft, const Message& message);

private:
    bool is_left; // 表示是否是左侧数据
};


// ------------------------------------------------------------ //
// --------------------- 文本消息 类 ------------------- //
// ------------------------------------------------------------ //
class MessageContentLabel : public QWidget
{
    Q_OBJECT
public:
    MessageContentLabel(const QString& text, bool isLeft, const MessageType& messageType,
                        const QString& fileId, const QByteArray& content);

    void paintEvent(QPaintEvent* event) override;
    void mousePressEvent(QMouseEvent* event) override;

    void updateUI(const QString& fileId, const QByteArray& fileContent);
    void saveAsFile(const QByteArray& content);

    void playDone();

    void contextMenuEvent(QContextMenuEvent* event) override;
    void speechConvertTextDone(const QString& fileId, const QString& text);

private:
    QLabel* label;
    bool isLeft;
    MessageType messageType;
    QString fileId;
    QByteArray content;

    bool loadContentOver = false;
};

class MessageImageLabel : public QWidget
{
    Q_OBJECT
public:
    MessageImageLabel(const QString& fileId, const QByteArray& content, bool isLeft);

    void updateUI(const QString& fileId, const QByteArray& content);
    void paintEvent(QPaintEvent* event) override;

private:
    QPushButton* imageBtn;
    QString fileId;
    QByteArray content;
    bool isLeft;
};

#endif // MESSAGEEXHIBITION_H
